package com.zhuss.meeting.processor;

import com.zhuss.bean.bo.meeting.apply.CancelApplyBo;
import com.zhuss.bean.vo.meeting.apply.DetailApplyVo;
import com.zhuss.common.AbstractProcessor;
import com.zhuss.common.Processor;
import com.zhuss.db.entity.OpLog;
import com.zhuss.enums.LogActionEnum;
import com.zhuss.enums.MoudleEnum;
import com.zhuss.exception.WpsException;
import com.zhuss.meeting.db.dao.ext.MeetingApplyDao;
import com.zhuss.meeting.db.dao.ext.MeetingRoomTimeDao;
import com.zhuss.meeting.db.entity.MeetingRoomApply;
import com.zhuss.meeting.db.entity.MeetingRoomTime;
import com.zhuss.util.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @Author: zhushanshuo
 * @Date: 2020/3/16 10:01
 */
@Component("applyCancel")
public class ApplyCancel extends AbstractProcessor<Object, CancelApplyBo> implements Processor<Object, CancelApplyBo> {

    @Autowired
    private MeetingApplyDao meetingApplyDao;

    @Autowired
    private MeetingRoomTimeDao meetingRoomTimeDao;

    @Autowired
    private ApplyRead applyRead;

    @Override
    public Object process(CancelApplyBo cancelApplyBo) throws WpsException {
        Long applyId = cancelApplyBo.getApplyId();
        String name = ((DetailApplyVo) applyRead.detail(applyId)).getRoomName();

        MeetingRoomApply m = new MeetingRoomApply();
        m.setApplyId(applyId);
        m.setStatus(cancelApplyBo.getStatus());

        m.setModifierId(cancelApplyBo.getConcurrentUserId());
        m.setModifier(cancelApplyBo.getConcurrentUserName());
        m.setModifyTime(new Date());
        meetingApplyDao.updateByPrimaryKeySelective(m);

        MeetingRoomTime time = new MeetingRoomTime();
        time.setRoomId(cancelApplyBo.getRoomId());
        time.setMeetingStartTime(cancelApplyBo.getStartTime());
        time.setMeetingEndTime(cancelApplyBo.getEndTime());

        time.setModifier(cancelApplyBo.getConcurrentUserName());
        time.setModifierId(cancelApplyBo.getConcurrentUserId());
        time.setModifyTime(new Date());
        time.setDeleted(301);
        meetingRoomTimeDao.delete(time);
        cancelApplyBo.setName(name);
        return null;
    }

    @Override
    public Object log(CancelApplyBo param) throws WpsException {
        OpLog record = new OpLog();
        record.setMoudle(MoudleEnum.会务.key());
        record.setAction(LogActionEnum.会议室取消使用.key());
        record.setTargetId(param.getApplyId());
        record.setTargetName(param.getName());
        record.setContent("参数: " + JsonUtil.toJson(param));
        record.setReason(param.getExceptionReason());

        record.setCreatorId(param.getConcurrentUserId());
        record.setCreator(param.getConcurrentUserName());
        record.setCreateTime(new Date());
        log(record);
        return null;
    }

    @Override
    public void check(CancelApplyBo cancelApplyBo) throws WpsException {

    }
}
